
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import javax.swing.JLabel;
import javax.swing.JPanel;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Riso-Desktop
 */
public class sliepka_leti extends JLabel implements Runnable, MouseListener {

    Thread let = new Thread(this);
    boolean mrtva;
    boolean pada;
    skore s;
    JPanel j;
    Hra h = new Hra();

    public sliepka_leti(JPanel j) {
        mrtva = false;
        pada = false;
        this.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/morhuhntL.gif")));
        int y = (int) (20 + Math.random() * 400);
        this.setBounds(1000, y, 95, 83);
        this.j = j;
        let.start();
        addMouseListener(this);
         }

            
    public void killed(){
        AudioPlayer ap = AudioPlayer.player;
        AudioStream as;
        AudioData ad;
        AudioDataStream zvuk = null;
        try {
            ;
            as = new AudioStream(new FileInputStream("zvuky/killChicken.wav"));
            ad = as.getData();
            zvuk = new AudioDataStream(ad);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ap.start(zvuk);
    
    }

    int smer = 0;

    public void pohyb() {
        if ((int) (1 + Math.random() * 100) < 50) {
            smer = 1;
        } else {
            smer = -1;
        }
        this.setLocation(getX() - 1, getY() + smer);
    }

    public void pohybd() {
        this.setLocation(getX(), getY() + 2);
    }

    @Override
    public void run() {
        while (pada != true) {
            try {
                pohyb();
                if (this.getX() < -150) {
                    j.remove(this);
                    mrtva = true;
                    pada = true;
                }
                let.sleep(5);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        while (mrtva != true && pada == true) {
            try {
                pohybd();
                if (this.getY() >= 430) {
                    j.remove(this);
                    mrtva = true;
                }
                let.sleep(10);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == e.BUTTON1 && h.strely >= 2) {
            
            this.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/morhuhntMrtvaL.png")));
            if (pada != true) {
                s = new skore(this.getX(), this.getY(), j);
                j.add(s);
                h.skore += 70;
                killed();
                pohybd();
            }
            pada = true;
        }
        if (e.getButton() == e.BUTTON3 && h.strely <= 1) {
            h.nabi();
        }
        h.mousePressed(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
